package com.zengjianjun.es.demo.highlevelclient.example.service.impl;

import com.alibaba.fastjson2.JSON;
import com.zengjianjun.es.demo.common.EsConstant;
import com.zengjianjun.es.demo.highlevelclient.EsClient;
import com.zengjianjun.es.demo.highlevelclient.example.builder.EsActiveQuery;
import com.zengjianjun.es.demo.highlevelclient.example.service.HLEsActiveService;
import com.zengjianjun.es.demo.highlevelclient.options.ConditionOption;
import com.zengjianjun.es.demo.highlevelclient.param.EsQueryParam;
import com.zengjianjun.es.demo.highlevelclient.param.FieldValue;
import com.zengjianjun.es.demo.highlevelclient.param.ParamQueryBuilder;
import com.zengjianjun.es.demo.param.EsActiveParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zengjianjun
 */
@Slf4j
@Service
public class HLEsActiveServiceImpl implements HLEsActiveService {

    @Resource
    private EsClient esClient;

    @Override
    public void aggregation(EsActiveParam param, String offerEventsEs) {
        // 构建分组聚合
        TermsAggregationBuilder aggregation = AggregationBuilders
                .terms("publisherIdAggs")
                .field("publisherId")
                .size(20)
                .subAggregation(
                        AggregationBuilders
                                .terms("subAffIdAggs")
                                .field("subAffId")
                                .size(5)
                                .subAggregation(
                                        AggregationBuilders
                                                .filter("eventFilter", QueryBuilders.termQuery("event", StringUtils.isBlank(offerEventsEs) ? new Object[]{""} : offerEventsEs.split(",")))
                                                .subAggregation(
                                                        AggregationBuilders
                                                                .terms("eventAnyAggs")
                                                                .field("event")
                                                )
                                )
                                .subAggregation(
                                        AggregationBuilders.filter("eventFilterInstall", QueryBuilders.termQuery("event", "install"))
                                )
                                .subAggregation(
                                        AggregationBuilders.filter("eventFilterReattribution", QueryBuilders.termQuery("event", "reattribution"))
                                )
                );
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        ParamQueryBuilder.paramBoolQueryBuilder(queryBuilder, this.getQuery(param));
        // 执行查询并返回结果
        SearchResponse searchResponse = esClient.aggregationByBoolQuery(EsConstant.INDEX_ACTIVE, queryBuilder, aggregation, 0);
        log.info("{}", JSON.toJSONString(searchResponse));
    }

    private EsQueryParam getQuery(EsActiveParam param) {
        EsActiveQuery.EsActiveQueryBuilder builder = EsActiveQuery.builder();
        if (param.getClickId() != null) {
            builder.clickId(FieldValue.<String>builder().value(param.getClickId()).conditionOption(ConditionOption.prefix).build());
        }
        if (param.getOfferId() != null) {
            builder.offerId(FieldValue.<Integer>builder().value(param.getOfferId()).conditionOption(ConditionOption.equal).build());
        }
        if (param.getPublisherId() != null) {
            builder.publisherId(FieldValue.<Integer>builder().value(param.getPublisherId()).conditionOption(ConditionOption.equal).build());
        }
        if (param.getSubAffId() != null) {
            builder.subAffId(FieldValue.<String>builder().value(param.getSubAffId()).conditionOption(ConditionOption.equal).build());
        }
        return builder.build();
    }
}
